package com.bbx.taxi.client.Adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.baidu.mapapi.search.core.PoiInfo;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.AddressDB;
import com.bbx.taxi.client.DB.Util.DBcolumns;

public class EditAddressAdapter extends BaseAdapter {
	private final int MSG_COLLECTFULL = 0;
	private final int MSG_COLLECT = 1;
	private final int MSG_HISTORY = 2;
	public static  final int TYPE_GEOCODER = 0;
	public static  final int TYPE_SEARCH = 1;
	public static  final int TYPE_COLLECT = 2;
	private List<PoiInfo> list = new ArrayList<PoiInfo>();
	private Context main;
	/** 类型0为地理编码geocoder，1为城市关键字搜索search ,2为收藏地址跟历史地址混合 */
	private int type = TYPE_COLLECT;
	private AddressDB db_address;// 地址数据库
	/** 收藏地址和历史地址的所有地址 */
	private List<Map<String, String>> list_address;

	public EditAddressAdapter(Context context, List<PoiInfo> list, int type) {
		this.list = list;
		this.main = context;
		this.type = type;
	}

	public EditAddressAdapter(Context context,
			List<Map<String, String>> list_address, int type,
			AddressDB db_address) {
		this.list_address = list_address;
		this.main = context;
		this.db_address = db_address;
	}

	@Override
	public int getCount() {
		if (type == TYPE_COLLECT) {
			return list_address == null ? 0 : list_address.size();
		} else {
			return list == null ? 0 : list.size();
		}

	}

	@Override
	public Object getItem(int position) {
		if (type == TYPE_COLLECT) {
			return list_address.get(position);
		} else {
			return list.get(position);
		}

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(main);
			convertView = (View) inflater.inflate(R.layout.item_editaddress,
					null);
			viewholder = new ViewHolder();
			viewholder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_name);
			viewholder.tv_current = (TextView) convertView
					.findViewById(R.id.tv_current);
			viewholder.tv_address = (TextView) convertView
					.findViewById(R.id.tv_address);
			viewholder.cb_collection = (CheckBox) convertView
					.findViewById(R.id.cb_collection);
			if (type != TYPE_COLLECT) {
				viewholder.cb_collection.setVisibility(View.INVISIBLE);
			}
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();

		if (type == TYPE_COLLECT) {
			if (list_address.get(position).get(DBcolumns.ADDRESS_COLLECTION)
					.equals(Value.ADDRESS_TYPE_COLLECTE)) {
				viewholder.cb_collection.setChecked(true);
			}
			else{
				viewholder.cb_collection.setChecked(false);
			}
//			viewholder.tv_address.setVisibility(View.GONE);
			viewholder.tv_name.setText(list_address.get(position).get(
					DBcolumns.ADDRESS_NAME));
			viewholder.tv_address.setText(list_address.get(position).get(
					DBcolumns.ADDRESS_ADDRESS));
			viewholder.tv_current.setVisibility(View.GONE);
		} else {
			viewholder.tv_name.setText(list.get(position).name);
			viewholder.tv_address.setText(list.get(position).address);
			viewholder.tv_address.setVisibility(View.VISIBLE);
			if(type == TYPE_GEOCODER&&position==0){
				viewholder.tv_name.setTextColor(main.getResources().getColor(R.color.blue));
				viewholder.tv_current.setVisibility(View.VISIBLE);
			}else{
				viewholder.tv_name.setTextColor(main.getResources().getColor(R.color.black));
				viewholder.tv_current.setVisibility(View.GONE);
			}
	
		}

		viewholder.cb_collection
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(final CompoundButton v,
							boolean isCheck) {
						if (!v.isPressed())
							return;
						if (isCheck) {// 收藏地址
							if (db_address.isCollectCountFull(main)) {
								mHandler.postDelayed(new Runnable() {

									@Override
									public void run() {
										v.setChecked(false);
									}
								}, 200);

								mHandler.sendEmptyMessage(MSG_COLLECTFULL);
							} else {
								onUpdateDB(
										list_address.get(position).get(
												DBcolumns.ADDRESS_ID),
										Value.ADDRESS_TYPE_COLLECTE);
								mHandler.sendEmptyMessage(MSG_COLLECT);
							}

						} else {
							// 取消收藏
							onUpdateDB(
									list_address.get(position).get(
											DBcolumns.ADDRESS_ID),
									Value.ADDRESS_TYPE_HISTORY);
							mHandler.sendEmptyMessage(MSG_HISTORY);
						}
					}
				});
		return convertView;
	}

	public class ViewHolder {
		TextView tv_name, tv_address,tv_current;
		CheckBox cb_collection;
	}

	public void onUpdateDB(String rowId, String collection) {
		db_address.onUpdate(rowId, collection);
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_COLLECTFULL:
				Toast.makeText(main,
						"最多保存" + db_address.count_collect + "个常用地址",
						Toast.LENGTH_SHORT).show();
				break;
			case MSG_COLLECT:
				Toast.makeText(main, "收藏成功", Toast.LENGTH_SHORT).show();
				break;
			case MSG_HISTORY:
				Toast.makeText(main, "取消收藏", Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};
}